package com.greenline.search.report.util;

import com.greenline.search.report.entity.LogReportResult;
import com.greenline.search.report.entity.SearchWord;
import com.greenline.search.report.model.Configuration;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 14-2-17
 * Time: 上午11:33
 * To change this template use File | Settings | File Templates.
 */
public class FormatLogResultUtil {
    private static String newLine = "\n";
    private static String tagString = "\t";

    //格式化日志分析结果
    public static String getAllFormatedResult(LogReportResult reportResult){
        StringBuilder sb = new StringBuilder();
        sb.append("<table border=1 cellspacing=0 style=border-collapse:collapse>");
        sb.append("<tr><td colspan=9><center><h2>一周搜索数据排行榜</h2></center></td></tr>");
        sb.append(getFormatEveryEnity("搜索热门TOP"+ Configuration.EVERY_ENTITY_LIST_COUNT+"（按照搜索量倒排）"
                ,"搜索上升TOP"+ Configuration.EVERY_ENTITY_LIST_COUNT+"（按照上升位次倒排）",
                reportResult.getOverallRankedList(), reportResult.getOverallUpList()));

        sb.append(getFormatEveryEnity("热门疾病TOP"+ Configuration.EVERY_ENTITY_LIST_COUNT+"（按照搜索量倒排）",
                "上升疾病TOP"+ Configuration.EVERY_ENTITY_LIST_COUNT+"（按照上升位次倒排）",
                reportResult.getDiseaseRankedList(), reportResult.getDiseaseUpList()));

        sb.append(getFormatEveryEnity("热门医生TOP"+ Configuration.EVERY_ENTITY_LIST_COUNT+"（按照搜索量倒排）",
                "上升医生TOP"+ Configuration.EVERY_ENTITY_LIST_COUNT+"（按照上升位次倒排）",
                reportResult.getDoctorRankedList(), reportResult.getDoctorUpList()));

        sb.append(getFormatEveryEnity("热门科室TOP"+ Configuration.EVERY_ENTITY_LIST_COUNT+"（按照搜索量倒排）",
                "上升科室TOP"+ Configuration.EVERY_ENTITY_LIST_COUNT+"（按照上升位次倒排）",
                reportResult.getDepartmentRankedList(), reportResult.getDepartmentUpList()));

        sb.append(getFormatEveryEnity("热门医院TOP"+ Configuration.EVERY_ENTITY_LIST_COUNT+"（按照搜索量倒排）",
                "上升医院TOP"+ Configuration.EVERY_ENTITY_LIST_COUNT+"（按照上升位次倒排）",
                reportResult.getHospitalRankedList(), reportResult.getHospitalUpList()));

        sb.append(newLine);
        sb.append("</table>");
        return sb.toString();
    }

    public static String getFormatEveryEnity(String rankName,String upName,List<SearchWord> rankSearchWordList,
                                             List<SearchWord> upSearchWordList){
        StringBuilder sb = new StringBuilder();
        sb.append(newLine);
        sb.append("<tr><td colspan=9></td></tr>");
        sb.append("<tr>");
        sb.append("<td colspan=4><center><h3>");
        sb.append(rankName).append("</center></h3></td>");
        sb.append("<td></td>");
        sb.append("<td colspan=4><center><h3>");
        sb.append(upName).append("</h3></center></td>");
        sb.append("</tr>");
        sb.append(newLine);
        sb.append("<tr>");
        sb.append("<td>排名</td>");
        sb.append("<td>关键词</td>");
        sb.append("<td>搜索量</td>");
        sb.append("<td>上升位数</td>");
        sb.append("<td></td>");
        sb.append("<td>排名</td>");
        sb.append("<td>关键词</td>");
        sb.append("<td>搜索量</td>");
        sb.append("<td>上升位数</td>");
        sb.append("</tr>");
        sb.append(newLine);
        //遍历每个SearchWord

        for(int i = 0; i < rankSearchWordList.size(); i++){
            SearchWord rankSearchWord = rankSearchWordList.get(i);
            SearchWord upSearchWord = upSearchWordList.get(i);
            sb.append("<tr>");
            sb.append("<td>").append(i+1).append("</td>");
            sb.append("<td>").append(rankSearchWord.getText()).append("</td>");
            sb.append("<td>").append(rankSearchWord.getFrequency()).append("</td>");
            sb.append("<td>").append(rankSearchWord.getRankDifference()).append("</td>");
            sb.append("<td>").append("").append("</td>");
            sb.append("<td>").append(i+1).append("</td>");
            sb.append("<td>").append(upSearchWord.getText()).append("</td>");
            sb.append("<td>").append(upSearchWord.getFrequency()).append("</td>");
            sb.append("<td>").append(upSearchWord.getRankDifference()).append("</td>");
            sb.append("</tr>");
            sb.append(newLine);
        }

        sb.append("<tr><td colspan=9></td></tr>").append(newLine);
        return sb.toString();
    }

    //格式化每个排名的列表
    public static String getFormatLogResult(List<SearchWord> searchWordList, boolean flag){
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < searchWordList.size(); i++){
            SearchWord word = searchWordList.get(i);
            sb.append(i+1).append(tagString);
            sb.append(word.getText()).append(tagString);
            sb.append(word.getFrequency()).append(tagString);
            if(flag){
                sb.append(word.getCurrentWeekRank());
            }else {
                sb.append(word.getRankDifference());
            }
            sb.append(newLine);
        }
        sb.append(newLine);
        return sb.toString();
    }
}
